﻿using SosForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SosData;

namespace UOCmaker
{
    public partial class FormAviso : FormBase
    {

        sosContext db;

        #region " Constructor "

        /// <summary>
        /// Contructor
        /// </summary>
        public FormAviso()
        {
            InitializeComponent();
            db = new sosContext();
            AvisobindingSource.DataSource = (from a in db.avisos.Where(a => a.CreatedByUserId == Parameters.UserId || a.usuarios.Contains(Parameters.UserNickName)) select a).ToList();
            Añadir();
        }

        /// <summary>
        /// Contructor
        /// </summary>
        public FormAviso(Aviso aviso)
        {
            InitializeComponent();
            db = new sosContext();
            aviso = (from a in db.avisos.Where(a => a.Id == aviso.Id) select a).FirstOrDefault();
            AvisobindingSource.DataSource = aviso;
        }

        #endregion

        #region " Metodos "

        /// <summary>
        /// Anterior
        /// </summary>
        private void Anterior()
        {
            this.AvisobindingSource.MovePrevious();
        }

        /// <summary>
        /// siguiente
        /// </summary>
        private void Siguiente()
        {
            this.AvisobindingSource.MoveNext();
        }

        Aviso aviso;

        /// <summary>
        /// Añadir aviso
        /// </summary>
        private void Añadir()
        {
            aviso = new Aviso();
            this.AvisobindingSource.Add(aviso);
            db.avisos.Add(aviso);
            AvisobindingSource.MoveLast();

        }

        /// <summary>
        /// Eliminar aviso
        /// </summary>
        private void Eliminar()
        {
            db.avisos.Remove((Aviso)AvisobindingSource.Current);
            this.AvisobindingSource.RemoveCurrent();
        }

        /// <summary>
        /// Guardar datos
        /// </summary>
        private void Guardar()
        {
            try
            {
                AvisobindingSource.EndEdit();
                db.SaveChanges();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

        /// <summary>
        /// Seleccionar color aviso
        /// </summary>
        private void SelectColor()
        {
            ColorDialog color = new ColorDialog();
            color.ShowDialog();
            this.lblColor.Text = color.Color.ToArgb().ToString();
            this.lblColor.ForeColor = color.Color;
            this.lblColor.BackColor = color.Color;
        }

        private void AñadirUsuarios()
        {
            FormSelecUsuariosAviso selecUsuariosFrm = new FormSelecUsuariosAviso();
            selecUsuariosFrm.ShowDialog();
            if (selecUsuariosFrm.usuarios != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string usuario in selecUsuariosFrm.usuarios)
                {
                    sb.Append(usuario + ",");
                    aviso.usuarios = sb.ToString().Substring(0, sb.Length - 1);
                    this.txtUsuarios.Text = aviso.usuarios;
                }
            }
        }

        #endregion

        #region " Controles "

        /// <summary>
        /// Guardar cambios
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butGuardar_Click(object sender, EventArgs e)
        {
            this.Guardar();
        }

        /// <summary>
        /// Siguiente nota
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butSiguiente_Click(object sender, EventArgs e)
        {
            Siguiente();
        }

        /// <summary>
        /// Anterior nota
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butAnterior_Click(object sender, EventArgs e)
        {
            Anterior();
        }

        /// <summary>
        /// Eliniar nota
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butEliminar_Click(object sender, EventArgs e)
        {
            Eliminar();
        }

        /// <summary>
        /// Añadir nota
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butAñadir_Click(object sender, EventArgs e)
        {
            Añadir();
        }


        /// <summary>
        /// Seleccionar color
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void lblColor_Click(object sender, EventArgs e)
        {
            SelectColor();
        }

        /// <summary>
        /// Añadir usuarios
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void butSelecUsuarios_Click(object sender, EventArgs e)
        {
            AñadirUsuarios();
        }
        #endregion

        #region " Eventos bindingSource "

        /// <summary>
        /// Current Changed bindinsource
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AvisobindingSource_CurrentChanged(object sender, EventArgs e)
        {
            aviso = (Aviso)AvisobindingSource.Current;
            this.lblColor.BackColor = Color.FromArgb(aviso.Color);
            this.lblColor.ForeColor = Color.FromArgb(aviso.Color);

            try
            {
                Usuario user = (from u in db.Users.Where(u => u.Id == aviso.CreatedByUserId) select u).FirstOrDefault();
                tsLblCreadoPor.Text = user.NickName;
            }
            catch { tsLblCreadoPor.Text = "-"; }

            tsLblFechaCreacion.Text = aviso.CreatedOn.ToString();
            this.Refresh();

        }

        #endregion

    }
}
